//
//  Weight.m
//  iWeighMyself
//
//  Created by Francisco Gindre on 8/4/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Weight.h"


@implementation Weight
@synthesize dateLogged;
@synthesize weightInPounds;
@synthesize weightInKilos;
@synthesize wasSent;
@synthesize weightID;


/*
 init the Weight object with all of it attributes
 */
-(id)initWithID:(NSInteger)aWeightID date:(NSDate*)dateWhenLogged weightInKilos:(NSNumber*)kilos weightInPounds:(NSNumber*) pounds isSent:(NSInteger)anIntAsBool;
{
	
	self=[super init];
	
	if (self){
		
		self.weightID = aWeightID;
		self.dateLogged = [dateWhenLogged retain];
		self.weightInPounds =[pounds retain];
		
		self.weightInKilos = [kilos retain];
		
		if (anIntAsBool) {
			self.wasSent = YES;
		}else {
			self.wasSent = NO;
			
		}

		
		
	}
	return self;
	
	
	
	
}
/*
 initializes a Weight object with pounds and date when it was logged
 */


-(id)initWithWeightInPounds:(NSNumber*) pounds date:(NSDate*) dateWhenLogged{
	
	self=[super init];
	
	if (self){
		self.dateLogged = [dateWhenLogged retain];
		self.weightInPounds =[pounds retain];
		
		weightInKilos = [[Weight kilosFromPounds:pounds] retain];
		
		self.wasSent = NO;
		
	}
	return self;
	
	
	
	
}


/*
 initializes a Weight object with kilos and date when it was logged
 */

-(id)initWithWeightInKilos:(NSNumber*) kilos date:(NSDate*) dateWhenLogged{
	self=[super init];
	
	if (self){
		self.dateLogged = [dateWhenLogged retain];
		self.weightInKilos =[kilos retain];
		
		weightInPounds = [[Weight poundsFromKilos:kilos] retain];
		
	}
	return self;
	
}

/* 
 converts a number of kilos into pounds
 */
+(NSNumber*)poundsFromKilos:(NSNumber*)kilos{
	NSNumber *returnValue = [NSNumber numberWithFloat:(2.204*[kilos floatValue])];
	return returnValue;
}

/* 
 converts a number of pounds into kilos
 
 */
+(NSNumber*)kilosFromPounds:(NSNumber*)pounds{
	NSNumber *returnValue= [NSNumber numberWithFloat:(0.453*[pounds floatValue])];
	return returnValue;
	
}

-(void)setWeightInKilos:(NSNumber *)kilos {
	if (kilos!= weightInKilos);
	{
		[weightInKilos autorelease];
		weightInKilos = [kilos retain];
		
		[weightInPounds autorelease];
		weightInPounds= [[Weight poundsFromKilos:kilos]retain];
	}
	
}

-(void) setWeightInPounds:(NSNumber *)pounds {
	if (pounds!= weightInPounds);
	{
		[weightInPounds autorelease];
		weightInPounds= [pounds retain];
		
		[weightInKilos autorelease];
		weightInKilos= [[Weight kilosFromPounds:pounds]retain];
	}	
}

/*
 get the unit of the weight in pounds EG: for 45.4 returns 45
 */
-(NSNumber*)getUnitsInPounds {
	return [NSNumber numberWithInt:[weightInPounds intValue]];
}
/*
 get the unit of the weight in pounds EG: for 45.4 returns 4
 */
-(NSNumber*)getDecimalInPounds {
	float total= [weightInPounds floatValue];
	float units= [weightInPounds intValue];
	total-=units;

	total*=10;

	return [NSNumber numberWithFloat:total];
}
/*
 get the unit of the weight in kilos EG: for 45.4 returns 45
 */
-(NSNumber*)getUnitsInKilos {
	return [NSNumber numberWithInt:[weightInKilos intValue]];
}
		
/*
 get the unit of the weight in kilos EG: for 45.4 returns 4
 */
-(NSNumber*)getDecimalInKilos{
	float total= [weightInKilos floatValue];
	float units= [weightInKilos intValue];
	total-=units;
	
	total*=10;
	//NSLog(@"%f total",total);
	return [NSNumber numberWithFloat:total];
}

/*
 set if this weight was sent or not
 */

-(void)setIsSent:(BOOL) isSent {
	
	self.wasSent = isSent;
}

/*
 get if the weight was sent or not
 */

-(BOOL)wasWeightSent {
	return self.wasSent;
}

/* 
 a weight to a string
 */
-(NSString*)weightToString {
	return [NSString stringWithFormat:@"id:%d \ndate:%@\nkilos:%f\npounds:%f\nsent?%d\n",
			self.weightID,
			[NSDateFormatter localizedStringFromDate:self.dateLogged dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle],
			[self.weightInKilos floatValue],
			[self.weightInPounds floatValue],
			self.wasSent];
}
- (void)dealloc {
    [super dealloc];
}


@end
